<?php
namespace Home\Model;
use Think\Model;
/**
* 活动模型
*/
class ActivityModel extends Model{

    //如果没有记录，说明没有报过名，
	//如果活动是三小时之前开始的,查看是否已完成，  
    public function moreThanThreeHour($activity){
        if ($activity['status'] == 9 || $activity['status'] == 8) {
            $status = 16;
        }elseif($activity['status'] == 1){
            $status = 6;
        }else{
            if($activity['status'] == 0){
                $status = 0;
            }else{
                $status = 1;
            }
        }
        return $status;
    }

    public function bmDetail($user_id,$activity){
        $userModel = M('user');
        $user = $userModel->field('id as user_id,username,is_verify,sex,age,photo_id,star,lat,lng')
                            ->where('id='.$activity['user_id'])->find();
        $my_id = $user_id;
        
            //获取报名人信息
        $bm_user = $userModel->field('id as bm_user_id,username as bm_username,photo_id as bm_photo_id')
                             ->where('id='.$my_id)->find();
        $is_verify = $userModel->where('id='.$my_id)->getField('is_verify');//是否认证

        //$activity['distance'] = getdistance($user['lng'],$user['lat'],$activity['lng'],$activity['lat']);
        $acti_log = M('activity_log')->where(array('user_id'=>$my_id,'activity_id'=>$activity['id']))->find();
        //如果还没有操作过
    }
    //获取报名本活动的用户
    public function getBmUserList($where){
        $list = M('activity_log a')
            ->field('a.last_time,a.is_arrive,a.is_gift,a.status,a.is_invited,b.id,b.username,b.age,b.sex,b.photo_id,b.is_verify')
            ->join('left join zm_user b on a.user_id=b.id')
            ->where($where)
            ->select();
        return $list;
    }
    //获取请求查看资料和报名的人的
    public function getFollowAndPhoto($id){
        $res = M('activity_log a')
            ->field("a.user_id,a.status,a.not_photo,b.username")
            ->where(array('activity_id'=>$id,'not_photo'=>0,'status'=>array('in',array(2,8))))
            ->join("left join zm_user b on a.user_id=b.id")
            ->find();
        if($res){
            $askfor_array = array(
                'ask_user_id'=>$res['user_id'],
                'ask_username'=>$res['username']
            );
            if ($res['status'] == 8) {
                if ($res['not_photo'] == 1) {
                    $is_askfor = 0;
                    $askfor_array = array();
                }
                else{
                    $askfor_array['ask_status'] = 8;
                }
            }
            elseif($res['status'] == 2){
                $askfor_array['ask_status'] = 2;
            }
        }
        return $askfor_array;
    }

    
    public function notLogStatus($activity,$relations,$is_verify){
        //如果活动是之前开始的,查看是否已完成，  
        if ($activity['start_time'] < time()-3*60*60) {
            //比如现在是10点，开始时间小于7点，已经开始3个小时以上了，即结束
            $status = $this->moreThanThreeHour($activity);
        }elseif($activity['start_time'] < time()){
            //在活动开始前三个小时之内
            if ($activity['status'] == 7 || $activity['status'] == 8) {
                $status = 5;
            }elseif($activity['status'] == 9){
                $status = 20;
            }elseif($activity['status'] == 11){
                $status = 21;
            }else{
                $status = 1;
            }
        }else{
            //如果是匿名发布
            if($activity['type'] == 0){
                $status = 1; //
            }else{
                //如果不是匿名发布
                //如果活动还没有开始,查看是否报满
                if ($activity['bao_ed'] == $activity['bao_num'] || $activity['join_ed'] != 0) {//报名满了或者已经邀约
                    $status = 4;  //已经报名，报名关闭
                }elseif($activity['status'] == 1){
                    //取消
                    $status = 6;
                }else{
                    //如果没有验证
                    if($is_verify == 0){
                        $status = 3;
                    }else{  
                        if ($relations) {
                            $status = 9;
                        }else{
                            $status = 7;
                        }
                    }
                }
            }
        }
        return $status;
    }

    /*活动已经开始了，
    *还没有人报名,活动过期
    *将用户的报名红包和见面礼红包返还给活动发起人
    */
    public function ReturnMoneyToDatedActivi($activity){
        $map = array(
            'user_id'       => $activity['user_id'],
            'activity_id'   => $activity['id'],
            'type'          => 106
        );
        $moneyLog = M('money_log')->where($map)->find();
        //活动已经开始了,并且没有报名,将用户的报名红包和见面礼红包还给本人
        if ($moneyLog) {
            //如果退款记录里面有
            $status = 0;$list=array();
        }else{
            $bao_num = $activity['bao_sum'];
            $money = $activity['bao_num'] * $activity['join_money'] + $activity['gift'];
            $redData = array(
                'money'=>$money,
                'user_id'=>$activity['user_id'],
                'activity_id'=>$activity['id'],
                'add_time'  => time(),
                'type'      => 106,
                'content'   => '未发放完的报名红包',
                'pay_type'  => 3
            );

            M('money_log')->add($redData);
            //通知用户他发起的活动已经过期，红包已经退回给他了
            $msg = array(
                'fa_id'     => 0,
                'shou_id'   => $activity['user_id'],
                'content'   => "您发布的活动已过期，报名红包已全额返回您的账户",
                'add_time'  => time(),
                'type'      => 0,
                'interval'  => intval(time()/(5*60))
            );
            M('message')->add($msg);
            M('user')->where('id='.$activity['user_id'])->setInc('money',$money);
            $status = 0;$list=array();
        }
        return $status;
    }

    //活动已经开始了，有人报名了但是没有被邀约，需要让报名的人得到红包
    //并且剩下的红包退回给发起人
    public function ReTurnGiftToDatedActivity($activity){
        //活动已经开始了，有人报名了但是没有被邀约，需要让报名的人得到红包
        //并且剩下的红包留下来给平台
        $map = array(
            'user_id'       => $activity['user_id'],
            'activity_id'   => $activity['id'],
            'type'          => 106
            );
        //查看是否退款，若已经退款，则直接跳过
        $moneyLog = M('money_log')->where($map)->find();

        if ($moneyLog) {
            $status = 0;
        }
        else{
            //查看剩余报名名额
            $shengxia = $activity['bao_num'] - $activity['bao_ed'];
            if ($shengxia > 0) {
                $money = $activity['join_money'] * $shengxia + $activity['gift'];
                $Data = array(
                    'money'=>$money,
                    'user_id'=>$activity['user_id'],
                    'activity_id'=>$activity['id'],
                    'add_time'  => time(),
                    'type'      => 106,
                    'content'   => '未发放完的报名红包和见面礼',
                    'pay_type'  => 3
                );
                M('user')->where('id='.$activity['user_id'])->setInc('money',$money);
                M('money_log')->add($Data);
                $msg = array(
                    'fa_id'     => 0,
                    'shou_id'   => $activi['user_id'],
                    'content'   => '您发布的活动已过期，未发放完的报名红包和见面礼已退回您的账户',
                    'add_time'  => time(),
                    'type'      => 0,
                    'interval'  => intval(time()/(5*60))
                );
                M('message')->add($msg);
                $status = 0;
            }
            else{ //如果报名报满了，将见面礼扣给平台
                $Data = array(
                    'money'=>$activity['gift'],
                    'user_id'=>$activity['user_id'],
                    'activity_id'=>$activity['id'],
                    'add_time'  => time(),
                    'type'      => 106,
                    'content'   => '退回见面礼'
                );
                M('user')->where('id='.$activity['user_id'])->setInc('money',$activity['gift']);
                M('money_log')->add($Data);
                $msg = array(
                    'fa_id'     => 0,
                    'shou_id'   => $activi['user_id'],
                    'content'   => '您发布的活动已过期，见面礼已退回您的账户',
                    'add_time'  => time(),
                    'type'      => 0,
                    'interval'  => intval(time()/(5*60))
                );
                M('message')->add($msg);
            }
            $status = 0;$list=array();
        }
        return $status;
    }

    //活动开始时间还没有到，若没人参加活动
    public function StatusOfNoneJoin($activity){
        //如果开始时间大于一小时，显示等待报名中
        if ($activity['start_time']-time()>30*60) {
            $status = 2;
        }elseif($activity['start_time']-time()<30*60 && $activity['start_time']-time()>0){
            //若果时间在一小时以内，则判断是不是已经延迟过了
            //没延迟返回3，
            if ($activity['is_delay'] == 0) {
                //先判断有没有人请求关注 
                $is_follow = M('activity_log')->where(array('activity_id'=>$id,'status'=>8))->find();
                $is_photo = M('activity_log')->where(array('activity_id'=>$id,'status'=>2,'not_photo'=>0))->find();
                if($is_follow || $is_photo){$status = 2;}else{$status = 3;}
            }
            //取消延迟返回4，
            if($activity['is_delay'] == 1){
                $status = 4;
            }
            //已经延迟了返回5
            if($activity['is_delay'] == 2){
                $status = 5;
            }
        }
        return $status;
    }

    public function dealBaomingList($list,$activi){
        $now = time();
        if(empty($list)){return array();}
        foreach ($list as $key => $value) {
            $is_friend = M('contact')->where(array('user1'=>$value['user_id'],'user2'=>$activi['user_id']))->find();
            if(!$is_friend && $value['is_invited'] == 2){
                $list[$key]['is_friend'] = 0;
            }else{
                $list[$key]['is_friend'] = 1;   
            }
            //将时间划分为三个阶段，活动开始前，活动开始之后到1个半小时，开始一个半小时以后
            //活动还没有开始
            if($now < $activi['start_time']){
                switch ($value['status']) {
                    case '11':
                        if($activi['join_ed'] == 0){
                            $list[$key]['str_status'] = '待邀约';
                        }else{
                            $list[$key]['str_status'] = '未被邀约';
                        }
                        break;
                    case '14':
                        $list[$key]['str_status'] = '已被邀约';
                        break;
                    case '12':
                        $list[$key]['str_status'] = '已退出活动';
                        break;
                    default:
                        break;
                }
            }elseif( $now < $activi['start_time'] + 90*60){
                //比如活动开始时间是晚上9点，时间就是十点半之前
                switch ($value['status']) {
                    case '11':
                        $list[$key]['str_status'] = '未被邀约';
                        break;
                    case '14':
                        if($value['is_gift'] == 1){
                            $list[$key]['str_status'] = '已领见面礼';
                        }elseif ($value['is_arrive'] == 1) {
                            $list[$key]['str_status'] = '已签到';
                        }else{
                            $list[$key]['str_status'] = '已被邀约';
                        }
                        break;
                    default:
                        
                        break;
                }
            }else{
                //活动开始一个半小时以上
                switch ($value['status']) {
                    case '11':
                        $list[$key]['bm_str_status'] = '未被邀约';
                        break;
                    case '14':
                        if($value['is_gift'] == 1){
                            $list[$key]['str_status'] = '已领见面礼';
                        }elseif ($value['is_arrive'] == 1) {
                            $list[$key]['str_status'] = '已签到';
                        }else{
                            $list[$key]['str_status'] = '爽约';
                        }
                        break;
                    case '16':
                        if($value['is_gift'] == 1){
                            $list[$key]['str_status'] = '已领见面礼';
                        }elseif ($value['is_arrive'] == 1) {
                            $list[$key]['str_status'] = '已签到';
                        }else{
                            $list[$key]['str_status'] = '爽约';
                        }
                        break;
                    case '18':
                        $list[$key]['str_status'] = '爽约';
                        break;
                    default:
                        break;
                }
            }
        }
        return $list;
    }

        //签到
    public function signIn(){
        $id = I('get.id') ? I('get.id') : $this->ajaxReturn(array('status'=>-1,'message'=>'活动不存在'));
        $user_id = get_user();
        //查找活动报名记录表里面的记录
        $logModel = M('activity_log');$actiModel = M('activity');
        $activi = $actiModel->where('id='.$id)->find();
        if ($activi['join_ed'] != 0) {
            $money = round(($activi['gift'] / $activi['join_ed']),2);
        }else{
            $this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
        }
        $log_where['user_id'] = $user_id;$log_where['activity_id']=$id;
        $ac_log = $logModel->where($log_where)->find();
        if(!$ac_log){
            //如果不存在记录
            $this->ajaxReturn(array('status'=>0,'message'=>'参数错误'));
        }
        //如果活动还没有点击确认到达
        if ($ac_log['is_arrive'] == 0) {
            $this->ajaxReturn(array('status'=>0,'message'=>'请先确认到达'));
        }
        //将活动记录的status改成 15 
        $res = $logModel->where($log_where)->save(array('status'=>15));
        //检查是否还有人没有签到，没有了就将发起活动的状态改成8
        $all_sign = $logModel->where(array('activity_id'=>$id,'status'=>14))->select();
        if (!$all_sign) {
            $actiModel->where('id='.$id)->save(array('status'=>8));
        }
        $now = time();
        //添加见面礼红包
        
        D('RedMoney')->getGift($id,$user_id,$money);
        if ($res === false) {
            $this->ajaxReturn(array('status'=>0,'message'=>'签到失败'));
        }
        $this->ajaxReturn(array('status'=>1,'message'=>'签到成功'));
    }



}